package com.Action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Bean.wdhtcustomerModel;
import com.Bean.wdreceptionrecordModel;
import com.Bean.wduserModel;
import com.Service.*;
@Controller
public class wdreceptionrecordAction {
@Autowired
        private wdreceptionrecordService wdreceptionrecordService;
@Autowired
       private wdcustomerService  wdcustomerService ;
@Autowired
       private wduserService wduserService;
//查询所有
@RequestMapping("/EmployeesManager.do")
public String list(Model model,wdreceptionrecordModel  wbean){
	 wbean.setDeleteState("1");
	// System.out.println("==========================="+wbean.getUserName());
	// System.out.println(wbean.getCustomerName());
	 if("".equals(wbean.getCustomerName())){
			wbean.setCustomerName(null);
		}
	 if("".equals(wbean.getUserName())){
			wbean.setUserName(null);
		}
	 if("".equals(wbean.getNumber())){
			wbean.setNumber(null);
		}
    List<wdreceptionrecordModel> list  = wdreceptionrecordService.getList(wbean);
    System.out.println("EmployeesManager.do"+list.size());
    System.out.println("EmployeesManager.do de list"+list);
    model.addAttribute("list", list);
	System.out.println(list);
    return "EmployeesManager";	
}

//查询放到添加的表单里
/*@RequestMapping("/selectrecord.do")
public String select(Model model,wdreceptionrecordModel  wbean){
	 wbean.setDeleteState("1");
	 String id = wbean.getCustomerId();
	 wdreceptionrecordModel wRRbean =  wdreceptionrecordService.selectadd(id);
	 model.addAttribute("wbean", wRRbean);
    return "addrecored";	
}*/

//把数据添加到数据库
@ RequestMapping("/addrecord.do")
public String addcustomer(wdreceptionrecordModel wbean) throws ParseException{
	
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	 //  System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
	   java.util.Date date=df.parse(df.format(new Date())); 
	   wbean.setReceptionTime(date);
	   wbean.setDeleteState("1");
	 //  System.out.println(wbean.getCustomerId());
	   wdhtcustomerModel cbean = new wdhtcustomerModel();
	   cbean.setCustomerId(wbean.getCustomerId());
	   String u = "已记录";
	   cbean.setDeleteUser(u);
	   int n = wdcustomerService.updatejilu(cbean);
	   
	 //  System.out.println("已经记录"+n);
	   int i = wdreceptionrecordService.insert(wbean);
	  System.out.println("添加成功"+i);
	  if(i==1){
	  JOptionPane.showMessageDialog(null, "添加成功", "标题", JOptionPane.ERROR_MESSAGE);
	  }
	  return "redirect:EmployeesManager.do";
}
//根据id查询到数据放到修改的表单里
@RequestMapping("updateReceptionRecord.do")
public String updateSelect(Model model,wdreceptionrecordModel wbean){
	//System.out.println(wbean.getReceptionRecordId());
	String id = wbean.getReceptionRecordId();
	wdreceptionrecordModel wRRbean =  wdreceptionrecordService.selectupdate(id);
	 model.addAttribute("wbean", wRRbean); 
	return "updateReceptionRecord";
}

//对数据进行修改
@RequestMapping("updaterecord.do")
public String updaterecord(wdreceptionrecordModel wbean) throws ParseException{
	 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	 //  System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
	   java.util.Date date=df.parse(df.format(new Date())); 
	   wbean.setUpdateTime(date);
	  // System.out.println("====================="+date);
	int i =  wdreceptionrecordService.updaterecord(wbean);
	//System.out.println("================"+i);
	return "redirect:EmployeesManager.do";
}
//根据id删除数据（修改数据把Deletestatue数据修改）
@RequestMapping("deleterecord.do")
public String deleterecord(wdreceptionrecordModel wbean){
//	System.out.println(wbean.getReceptionRecordId());
	String n = "0";
	wbean.setDeleteState(n);
	int i =wdreceptionrecordService.updaterecord(wbean);
	return "redirect:EmployeesManager.do";
}
//对业务员进行验证是否存在
@RequestMapping("/UserYanzheng.do")
public @ResponseBody Map<String, Object> toAjaxCustomer(wduserModel ubean){
	System.out.println("================"+ubean.getName());
	String name = ubean.getName();
	wduserModel umbean =  wduserService.selectuserName(name);
	if(umbean!=null){
    System.out.println("===================="+umbean.getName());
	}
    Map<String, Object> comMap = new HashMap<String, Object>();
	  comMap.put("user", umbean);
	return comMap;
}
}
